package com.gitee.wsl.doc.html

 interface KsoupHtmlHandler {

     fun onParserInit(ksoupHtmlParser: KsoupHtmlParser) {}

    /**
     * Reset the handler back to starting state
     */
     fun onReset() {}

    /**
     * Signal that the parser is done parsing the document
     */
     fun onEnd() {}

     fun onError(error: Exception) {}

     fun onCloseTag(name: String, isImplied: Boolean) {}

     fun onOpenTagName(name: String) {}

    /**
     *
     * @param name The name of the attribute
     * @param value The value of the attribute
     * @param quote The quotes used around the attribute. `null` if the attribute has no quotes around the value or if the attribute has no value.
     */
     fun onAttribute(
        name: String,
        value: String,
        quote: String? = null,
    ) {}

     fun onOpenTag(
        name: String,
        attributes: Map<String, String>,
        isImplied: Boolean,
    ) {}

     fun onText(text: String) {}

     fun onComment(comment: String) {}

     fun onCDataStart() {}

     fun onCDataEnd() {}

     fun onCommentEnd() {}

     fun onProcessingInstruction(
        name: String,
        data: String,
    ) {}

     object Default : KsoupHtmlHandler

     class Builder {

        private var handler: KsoupHtmlHandler = Default

         fun onParserInit(block: (ksoupHtmlParser: KsoupHtmlParser) -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onParserInit(ksoupHtmlParser: KsoupHtmlParser) {
                    block(ksoupHtmlParser)
                }
            }
            return this
        }

         fun onReset(block: () -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onReset() {
                    block()
                }
            }
            return this
        }

         fun onEnd(block: () -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onEnd() {
                    block()
                }
            }
            return this
        }

         fun onError(block: (error: Exception) -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onError(error: Exception) {
                    block(error)
                }
            }
            return this
        }

         fun onCloseTag(block: (name: String, isImplied: Boolean) -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onCloseTag(name: String, isImplied: Boolean) {
                    block(name, isImplied)
                }
            }
            return this
        }

         fun onOpenTagName(block: (name: String) -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onOpenTagName(name: String) {
                    block(name)
                }
            }
            return this
        }

         fun onAttribute(block: (name: String, value: String, quote: String?) -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onAttribute(name: String, value: String, quote: String?) {
                    block(name, value, quote)
                }
            }
            return this
        }

         fun onOpenTag(block: (name: String, attributes: Map<String, String>, isImplied: Boolean) -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onOpenTag(name: String, attributes: Map<String, String>, isImplied: Boolean) {
                    block(name, attributes, isImplied)
                }
            }
            return this
        }

         fun onText(block: (text: String) -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onText(text: String) {
                    block(text)
                }
            }
            return this
        }

         fun onComment(block: (comment: String) -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onComment(comment: String) {
                    block(comment)
                }
            }
            return this
        }

         fun onCDataStart(block: () -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onCDataStart() {
                    block()
                }
            }
            return this
        }

         fun onCDataEnd(block: () -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onCDataEnd() {
                    block()
                }
            }
            return this
        }

         fun onCommentEnd(block: () -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onCommentEnd() {
                    block()
                }
            }
            return this
        }

         fun onProcessingInstruction(block: (name: String, data: String) -> Unit): Builder {
            handler = object : KsoupHtmlHandler by handler {
                override fun onProcessingInstruction(name: String, data: String) {
                    block(name, data)
                }
            }
            return this
        }

         fun build(): KsoupHtmlHandler = handler

    }

}